package com.zzw.basic.classloader;

import java.net.URL;
import java.net.URLClassLoader;
import java.sql.Connection;
import java.sql.Driver;
import java.util.Properties;

public class URLClassLoaderTest {

	private static Connection conn;
	public static Connection getConn(String url,
			String user,String pass) throws Exception{
		if(conn == null){
			URL[] urls = {new URL("file:mysql-connector-java-5.1.30-bin.jar")};
			URLClassLoader mClassLoader = new URLClassLoader(urls);
			Driver driver = (Driver) mClassLoader.loadClass("com.mysql.jdbc.Driver").newInstance();
			Properties props = new Properties();
			props.setProperty("user", user);
			props.setProperty("password", pass);
			conn = driver.connect(url, props);
		}
		return conn;
	}
	
	public static void main(String[] args) {
		try {
			System.out.println(getConn("jdbc:mysql://localhost:3306/shiro3", "root", "root"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
